package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import demo.Customer;

/**
 * Servlet implementation class FirstServlet
 */
@WebServlet(
		urlPatterns = { "/FirstServlet","/customer" }, 
		initParams = { 
				@WebInitParam(name = "monnaie", value = "euro"), 
				@WebInitParam(name = "lang", value = "fr")
		})
public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String langue;
	private String monnaie;
	
	@Override
	public void init() throws ServletException {
		super.init();
		langue=getInitParameter("lang");
		monnaie = getInitParameter("monnaie");
	}
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FirstServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>");
		out.println("Test Servlet");
		out.println("</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("</body>");
		out.println("Hello ");
		try {
			Customer customer=(Customer) request.getSession().getAttribute("cust");
			
			if (customer==null){
				 customer = new Customer(1, "Dupont");
				 request.getSession().setAttribute("cust", customer); 
			}
			
			out.println(customer.getName());
		} catch (Exception e) {
			out.println("pas de customer dans le contexte de session");
		}
		out.println("<br>");
		out.println("langue="+langue);
		out.println("<br>");
		out.println("monnaie="+monnaie);
		out.println("<br>");
		out.println("date="+new Date());
		out.println("<br>");
	
		out.println("</html>");
	}


	
	

}
